import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -06 -22
 * Time: 22:53
 */
class Message {
    public String from;
    public String to;
    public String message;
}

@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    private ObjectMapper om = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        List<Message> messageList = load();
        String jsonResp = om.writeValueAsString(messageList);
        System.out.println("jsonResp: " + jsonResp);
        resp.getWriter().write(jsonResp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message msg=om.readValue(req.getInputStream(),Message.class);
        save(msg);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("dodelete");
        delete();
    }

    private List<Message> load() {
        List<Message> messageList = new ArrayList<>();
        Connection con = null;
        PreparedStatement stat = null;
        ResultSet res = null;
        try {
            con = DBUtil.getConnection();
            String sql = "select * from msgwall";//构建str
            stat = con.prepareStatement(sql);//构建sql
            res = stat.executeQuery();//执行sql
            while (res.next()) {//遍历
                Message msg = new Message();
                msg.from = res.getString("from");
                msg.to = res.getString("to");
                msg.message = res.getString("message");
                messageList.add(msg);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(con, stat, res);//释放内存
        }
        return messageList;
    }

    private void save(Message msg) {
        Connection con = null;
        PreparedStatement stat = null;
        try {
            con = DBUtil.getConnection();
            String sql = "insert into msgwall(`from`,`to`,message) value(?,?,?)";
            stat = con.prepareStatement(sql);
            stat.setString(1, msg.from);
            stat.setString(2, msg.to);
            stat.setString(3, msg.message);
            stat.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(con, stat, null);//释放内存
        }
    }
    private void delete() {
        Connection con = null;
        PreparedStatement stat = null;
        try {
            con = DBUtil.getConnection();
            //String sql = "delete from msgwall where id=(select id from msgwall order by id desc limit 1)";error
            String sql = "delete from msgwall where 1 order by id desc limit 1";
            stat = con.prepareStatement(sql);
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(con, stat, null);//释放内存
        }
    }
}
